package com.mes.controller.manu;

import com.mes.service.manu.KnittingNeeService;
import com.yy.base.page.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("KnittingNee")
public class KnittingNeeController {
    @Autowired
    private KnittingNeeService knittingNeeService;


    //开工完工按钮
    @ResponseBody
    @RequestMapping(value = "beginlist", method = RequestMethod.POST)
    public Message beginlist(HttpServletRequest request) {
        Message msg = new Message();
        String rfid = "";
        if (request.getParameter("rfid") != null && request.getParameter("rfid") != "") {
            rfid = request.getParameter("rfid");
        } else {
            msg.setData("results", "false");
            msg.setErrorMessage("[RFID]不能为空！");
            return msg;
        }

        String status = "";
        List<Map<String, Object>> Lists = knittingNeeService.getHistoryList(rfid);

        if(Lists.get(0).get("LASTSTATUS").equals("START")){
            status = "FINISHED";
            knittingNeeService.UpdateExeEndStatus(Lists.get(0).get("LASTEXEID").toString(),status);
        }else if(Lists.get(0).get("LASTSTATUS").equals("FINISHED")){
            status = "START";
            knittingNeeService.UpdateExeEndStatus(Lists.get(0).get("EXEID").toString(),status);
        }else if(Lists.get(0).get("LASTSTATUS").equals("STOP")){
            msg.setData("results", "false");
            msg.setErrorMessage("该工序暂停中不可完工！！");
            return msg;
        }


        return msg;
    }

    //加载工单信息
    @ResponseBody
    @RequestMapping(value = "rfidLists", method = RequestMethod.POST)
    public Message rfidLists(HttpServletRequest request) {
        Message msg = new Message();
        try {
            String rfid = "";
            if (request.getParameter("rfid") != null && request.getParameter("rfid") != "") {
                rfid = request.getParameter("rfid");
            } else {
                msg.setData("results", "false");
                msg.setErrorMessage("[RFID]不能为空！");
                return msg;
            }

            msg.setData("ExeLists", knittingNeeService.getjobExeStatusList(rfid));

            msg.setData("results", "true");
            msg.setOkMessage("处理成功！");
        } catch (Exception e) {
            msg.setData("results", "false");
            msg.setErrorMessage("处理失败！");
            e.printStackTrace();
        }
        return msg;
    }

    //暂停工序
    @ResponseBody
    @RequestMapping(value = "suspendLists", method = RequestMethod.POST)
    public Message suspendLists(HttpServletRequest request) {
        Message msg = new Message();
        String rfid = "";
        if (request.getParameter("rfid") != null && request.getParameter("rfid") != "") {
            rfid = request.getParameter("rfid");
        } else {
            msg.setData("results", "false");
            msg.setErrorMessage("[RFID]不能为空！");
            return msg;
        }

        String status = "";
        List<Map<String, Object>> Lists = knittingNeeService.getHistoryList(rfid);

        //判断上道工序是否为开工中，是则运行暂停上道工序
        if(Lists.get(0).get("LASTSTATUS").equals("START")){
            status = "STOP";
            knittingNeeService.UpdateExeEndStopStatus(Lists.get(0).get("LASTEXEID").toString(),status);
        //上道工序为已完工，当前工序未开工代表该工序不支持暂停
        }else if(Lists.get(0).get("LASTSTATUS").equals("FINISHED")){
            msg.setData("results", "false");
            msg.setErrorMessage("工序未开工，不支持暂停操作！");
        //判断上道工序是否为暂停中，是则运行重新启动并记录暂停时间
        }else if(Lists.get(0).get("LASTSTATUS").equals("STOP")) {
            status = "START";
            knittingNeeService.UpdateExeEndStatus(Lists.get(0).get("LASTEXEID").toString(), status);
        }


        return msg;
    }

    //根据作业工单号及工序号确认作业指导书
    @ResponseBody
    @RequestMapping(value = "selectZyBook", method = RequestMethod.POST)
    public Message selectZyBook(HttpServletRequest request) {
        Message msg = new Message();
        try {
            String rfid = "";
            if (request.getParameter("rfid") != null && request.getParameter("rfid") != "") {
                rfid = request.getParameter("rfid");
            } else {
                msg.setData("results", "false");
                msg.setErrorMessage("[RFID]不能为空！");
                return msg;
            }
            String processOpr_id = "0CF6FFFA4C62412CA78DD245F00F869D";

            //获取工艺作业指导书
            String fileId = knittingNeeService.getSopFileId(rfid,processOpr_id);
            msg.setData("fileId", fileId);
            msg.setData("results", "true");
            msg.setOkMessage("处理成功！");
        } catch (Exception e) {
            msg.setData("results", "false");
            msg.setErrorMessage("处理失败！");
            e.printStackTrace();
        }
        return msg;
    }
}
	
	
	
	
